<?php
namespace app\manager\controller;

use app\common\traits\controller\TraitController;
use think\Request;
use Carbon\Carbon;

abstract class BaseController extends \think\Controller
{
    protected $loginmanager = '';
    protected $beforeActionList = [
        'first',
        'checkLogin' => ['except' => 'getcheckcode,getlogin,getlogout,getforgetpassword,postsendpwd,postnewpwd,postlogin,']
    ];

    protected function init()
    {
        session(['prefix' => '','auto_start' => true]);
    }
    protected function checkLogin()
    {
    	if (!model('CampManagers')->checkLogin()) {
            $this->redirect('@campmanager/login');
            exit;
        }
        $this->loginmanager = model('CampManagers')->getLoginManagerInfo();
    }
    /**
     * 获取登录用户信息
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public function getLoginManagerInfo($field = '')
    {
        if ($field) {
            if (isset($this->loginmanager->$field)) {
                return $this->loginmanager->$field; 
            }
            return false;
        }
        return $this->loginmanager;
    }
    /**
     * 视图公共变量
     * @return [type] [description]
     */
    protected function first()
    {
        //get user info
        $this->assign('title','双辉');
        $this->assign('name','sanyc');
        $this->assign('f','sanyc');
    }
    protected function getApi($calss_name, $type = 'model', $module = 'user')
    {
        return new \Yar_Client($this->api_url.$this->createToken($calss_name, $type, $module));
    }
    /**
     * 创建api访问sign
     * @param  [type] $controller [description]
     * @param  string $module     [description]
     * @return [type]             [description]
     */
    protected function createToken($calss_name, $type = 'model', $module = 'user')
    {
        $lang = request()->langset();//获取系统语言类型
        $data = array_merge(Request::instance()->get(), ['t' => $type, 'c' => $calss_name, 'm' => $module, 'lang' => $lang]);
        $str = $this->mixSign($data);
        return "sign=".$str.'&'.http_build_query($data);
    }
    
    
    public function diffDays($date_one, $date_two)
    {
        return Carbon::createFromFormat('Y-n-j', $date_one)->diffInDays(Carbon::createFromFormat('Y-n-j', $date_two)) + 1;
    }
}
